﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebService.BUS;

namespace WebService
{
    public partial class Item : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
        }


        protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
        {

        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                TextBox txtItemName = (TextBox)e.Item.FindControl("txtItemName");
                DropDownList ddlCategory = (DropDownList)e.Item.FindControl("ddlCategoryID");
                TextBox txtRate = (TextBox)e.Item.FindControl("txtRate");
                TextBox txtAddress = (TextBox)e.Item.FindControl("txtAddress");
                TextBox txtPhone = (TextBox)e.Item.FindControl("txtPhone");
                TextBox txtEmail = (TextBox)e.Item.FindControl("txtEmail");
                TextBox txtNote = (TextBox)e.Item.FindControl("txtNote");
                TextBox txtX = (TextBox)e.Item.FindControl("txtX");
                TextBox txtY = (TextBox)e.Item.FindControl("txtY");
                TextBox txtImage = (TextBox)e.Item.FindControl("txtImage");
                TextBox txtIsDelete = (TextBox)e.Item.FindControl("txtIsDelete");
                TextBox txtCountRate = (TextBox)e.Item.FindControl("txtCountRate");
                TextBox txtChecked = (TextBox)e.Item.FindControl("txtChecked");

                string insertCommand = "INSERT INTO tbl_Items VALUES(N'" + txtItemName.Text + "','" + ddlCategory.SelectedValue
                    + "','0',N'" + txtAddress.Text + "','" + txtPhone.Text + "','" + txtEmail.Text + "',N'" + txtNote.Text
                    + "','" + txtX.Text + "','" + txtY.Text + "','" + txtImage.Text.Trim() + "','0','"+txtChecked.Text+"');";
                SqlDataSource1.InsertCommand = insertCommand;
            }
            else if (e.CommandName == "Update")
            {
                TextBox txtItemID = (TextBox)e.Item.FindControl("txtUpItemID");
                TextBox txtItemName = (TextBox)e.Item.FindControl("txtUpItemName");
                DropDownList ddlCategory = (DropDownList)e.Item.FindControl("ddlUpCategoryID");
                TextBox txtRate = (TextBox)e.Item.FindControl("txtUpRate");
                TextBox txtAddress = (TextBox)e.Item.FindControl("txtUpAddress");
                TextBox txtPhone = (TextBox)e.Item.FindControl("txtUpPhone");
                TextBox txtEmail = (TextBox)e.Item.FindControl("txtUpEmail");
                TextBox txtNote = (TextBox)e.Item.FindControl("txtUpNote");
                TextBox txtX = (TextBox)e.Item.FindControl("txtUpX");
                TextBox txtY = (TextBox)e.Item.FindControl("txtUpY");
                TextBox txtImage = (TextBox)e.Item.FindControl("txtUpImage");
                TextBox txtIsDelete = (TextBox)e.Item.FindControl("txtUpIsDelete");
                TextBox txtCountRate = (TextBox)e.Item.FindControl("txtUpCountRate");
                TextBox txtChecked = (TextBox)e.Item.FindControl("txtChecked");
                string updateCommand = "Update [tbl_Items] set [ItemName]=N'" + txtItemName.Text + "', [CategoryID]='" + ddlCategory.SelectedValue
                    + "', [Address]=N'" + txtAddress.Text + "', [Phone]='" + txtPhone.Text + "', [Email]='" + txtEmail.Text + "', [Note]=N'" + txtNote.Text
                    + "', [X]='" + txtX.Text + "', [Y]='" + txtY.Text + "', [Image]='" + txtImage.Text + "', [Checked]='" + txtChecked.Text + "' where ItemId='" + Convert.ToInt32(txtItemID.Text) + "';";
                SqlDataSource1.UpdateCommand = updateCommand;
            }
            else if (e.CommandName == "Delete")
            {
                TextBox txtId = (TextBox)e.Item.FindControl("txtUpItemId");
                string deleteCommand = "delete from [tbl_Items] where ItemId=" + Convert.ToInt32(txtId.Text);
                SqlDataSource1.DeleteCommand = deleteCommand;
            }
        }
    }
}